//
//  DoorEventView.m
//  RONGAO
//
//  Created by wei wang on 2017/2/23.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorEventView.h"
#import "Masonry.h"

#define ImageWith 104.5  //宽度加上↔️两边20 再加4个点
#define leftGapWith 64 //左右的宽度加4个点
@interface DoorEventView ()

/** 首次进入时👈图片的中心点位置 **/
@property(nonatomic,assign) CGFloat leftImageFistCentX;

/** 首次进入时👉图片的中心点位置 **/
@property(nonatomic,assign) CGFloat rightImageFistCentX;

/** stop **/
@property(nonatomic,assign) BOOL stop;


/** openTime **/
@property(nonatomic,strong) NSTimer *openTime;

/** closeTime **/
@property(nonatomic,strong) NSTimer *closeTime;

/** gapCount **/
@property(nonatomic,assign) NSInteger gapCount;

/** endCount **/
@property(nonatomic,assign) NSInteger endCount;

/** percentCount **/
@property(nonatomic,assign) float percentCount;

@end


@implementation DoorEventView



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init{
    
    if ([super init]) {
        
        self.backgroundColor = [UIColor blackColor];
        [self masLayoutSubview];
        self.gapCount = 0;

    }
    return self;
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self;
    __weak typeof(self) weakself = self;
    
    [self.envetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.edges.equalTo(superView);
        
    }];
    

    
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-10);
        make.left.mas_equalTo(superView.mas_left).mas_offset(20);
        make.right.mas_equalTo(superView.mas_right).mas_offset(-20);
        
    }];
    
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(weakself.bottomImageView.mas_top);
        make.width.mas_equalTo(80);
        make.right.mas_equalTo(weakself.bottomImageView.mas_centerX).mas_offset(-0.5);
        
        
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(weakself.bottomImageView.mas_top);
        make.width.mas_equalTo(80);
        make.left.mas_equalTo(weakself.bottomImageView.mas_centerX).mas_offset(0.5);
        
        
    }];
    
}

- (void)getImageCentX{
    
    self.leftImageFistCentX = self.leftImageView.center.x;
    self.rightImageFistCentX = self.rightImageView.center.x;
}

- (void)openTimeAction{
    
    [self stopAnimation];
    self.openTime = [NSTimer scheduledTimerWithTimeInterval:0.05f target:self selector:@selector(action:) userInfo:nil repeats:YES];

}

- (void)action:(CGFloat)offset{
    
    self.gapCount += 2;

    CGFloat gap = (self.bottomImageView.center.x-ImageWith) * self.percentCount;

    
    
    if (self.endCount > 0) {
        
        if (self.gapCount >= self.endCount+gap || self.gapCount - (self.bottomImageView.center.x-ImageWith) > 0) {//防止越界
            
            [self.openTime invalidate];
            self.openTime = nil;
            self.endCount = self.gapCount;
            return;

        }
        
    }else{
        
        if (self.gapCount >= gap) {//防止越界
            
            [self.openTime invalidate];
            self.openTime = nil;
            self.endCount = self.gapCount;
            return;
        }

    }
    
    
    self.leftImageView.transform = CGAffineTransformMakeTranslation(-(self.gapCount), 0);
    self.rightImageView.transform = CGAffineTransformMakeTranslation(self.gapCount, 0);
}


- (void)closeTimeAction{
    
    [self stopAnimation];
    self.closeTime = [NSTimer scheduledTimerWithTimeInterval:0.05f target:self selector:@selector(Closeaction) userInfo:nil repeats:YES];

}

- (void)Closeaction{
    
    self.gapCount -= 2;
    CGFloat gap = (self.bottomImageView.center.x-ImageWith) * self.percentCount;
    
    
    
    if (self.endCount > 0) {
        
        if (self.gapCount < 0) {//防止越界
            
            [self.closeTime invalidate];
            self.closeTime = nil;
            self.endCount = self.gapCount;
            return;
            
        }
        
    }else{
        
        if (self.gapCount <= gap) {//防止越界
            
            [self.closeTime invalidate];
            self.closeTime = nil;
            self.endCount = 0;
            return;
        }
        
    }
    
    
    self.leftImageView.transform = CGAffineTransformMakeTranslation(-self.gapCount, 0);
    self.rightImageView.transform = CGAffineTransformMakeTranslation((self.gapCount), 0);

}


- (void)stopAnimation{
    
    
    [self.openTime invalidate];
    [self.closeTime invalidate];
    
    self.openTime = nil;
    self.closeTime = nil;
    self.endCount = self.gapCount;
}


- (void)setChangeAnimation:(NSUInteger)minCount{
    
    
    [UIView animateWithDuration:3 animations:^{
        
        [self.leftImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.mas_equalTo(self.bottomImageView.mas_top);
            make.left.mas_equalTo(self.bottomImageView.mas_left).mas_offset(4);
            
            
        }];
        
        [self.rightImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.mas_equalTo(self.bottomImageView.mas_top);
            make.right.mas_equalTo(self.bottomImageView.mas_right).mas_offset(-4);
            
            
        }];
        
        [self layoutIfNeeded];
        
    }];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((3+minCount) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:3 animations:^{
            
            [self.leftImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.bottom.mas_equalTo(self.bottomImageView.mas_top);
                make.right.mas_equalTo(self.bottomImageView.mas_centerX).mas_offset(-0.5);
                
                
            }];
            
            [self.rightImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.bottom.mas_equalTo(self.bottomImageView.mas_top);
                make.left.mas_equalTo(self.bottomImageView.mas_centerX).mas_offset(0.5);
                
                
            }];
            
            [self layoutIfNeeded];
            
        }];
        
        
        
    });
    
    
    
    
}



- (void)moveLeftWithOffset: (CGFloat)offset completion: (void(^)(BOOL finished))completion duration: (NSTimeInterval)duration{
    
    self.percentCount = offset;
    [self openTimeAction];
    
}

- (void)moveRightWithOffset: (CGFloat)offset completion: (void(^)(BOOL finished))completion duration: (NSTimeInterval)duration{
  
    self.percentCount = offset;
    [self closeTimeAction];
    
}

- (UIButton *)envetBtn{
    
    if (!_envetBtn) {
        
        _envetBtn = [[UIButton alloc] init];
        [self addSubview:_envetBtn];

    }
    
    return _envetBtn;
}

- (UIImageView *)bottomImageView{
    
    if (!_bottomImageView) {
        
        
        _bottomImageView = [[UIImageView alloc] init];
        _bottomImageView.image = [UIImage imageNamed:@"icon_door_under"];
        
        [self addSubview:_bottomImageView];
    }
    
    return _bottomImageView;
}


- (UIImageView *)leftImageView{
    
    if (!_leftImageView) {
        
        _leftImageView = [[UIImageView alloc] init];
        _leftImageView.image = [UIImage imageNamed:@"icon_door_letf"];
        
        [self addSubview:_leftImageView];
    }
    
    return _leftImageView;
}

- (UIImageView *)rightImageView{
    
    if (!_rightImageView) {
        
        _rightImageView = [[UIImageView alloc] init];
        _rightImageView.image = [UIImage imageNamed:@"icon_door_right-"];
        
        [self addSubview:_rightImageView];
    }
    
    return _rightImageView;
}

- (void)dealloc
{
    
}
@end
